package view;

import controller.Controller;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

/**
 * Created by Pavel Nikolaev
 */
public class SearchPanel extends JPanel implements PropertyChangeListener {
    private JTextField myTextField;

    public SearchPanel() {
        setMinimumSize(new Dimension(200, 40));
        setLayout(new BorderLayout());
        add(new JLabel("Enter word: "), BorderLayout.WEST);
        myTextField = new JTextField();
        myTextField.setEnabled(false);
        add(myTextField, BorderLayout.CENTER);
    }

    public void subscribe(final Controller controller) {
        myTextField.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                controller.searchText(myTextField.getText());
            }
        });
    }

    public void propertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals("DocumentUpdated")) {
            myTextField.setEnabled(true);
        }
    }
}
